<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Rep\Rbac\UserRep;
use Illuminate\Http\Request;

class Login extends Controller
{
    public $rep;
    public function __construct()
    {
        $this->rep = new UserRep();
    }

    /**
     * 手机短信验证码
     * 登录
     */
    public function signIn(Request $request)
    {
        $phone = $request->get("phone");
        $verification_code = $request->get("verification_code");
        $res = $this->rep->signIn($phone,$verification_code);

        return re($res['code'],$res['data'],$res['msg']);
    }

    /**
     * 账号密码登陆
     */
    public function signInPwd(Request $request)
    {
        //p($request->all());
        $parent_username = $request->input("parent_username");
        $password = $request->input("password");
        $is_primary = $request->input("is_primary");
        $child_username = $request->input("child_username");
        //$verification_code = $request->input("verification_code");
        $res = $this->rep->signInPwdRep($parent_username,$child_username,$password,$is_primary);

        return re($res['code'],$res['data'],$res['msg']);
    }

    /**退出登录**/
    public function signOut(Request $request){
        $user_info = $request->get("user_info");
        $token = $request->header("token");
        $user_id = $request->get("user_id");
        UserRep::removeToken($token);
        UserRep::removeDBToken($user_id);
        return re(0);
    }

    /**
     * 注册
     */
    public function regist(Request $request){
        $params = $request->only('account','phone','province','city','county','address','contact','contact_phone');
        $res = $this->rep->registerRep($params);
        return re($res['code'],$res['data'],$res['msg']);
    }

    /**
     * 发送手机验证码
     */
    public function sendSMS(Request $request){
        $phone = $request->input('phone');
        $res = $this->rep->sendSMSRep($phone);
        return re($res['code'],$res['data'],$res['msg']);
    }

    /**
     * 密码修改
     */
    public function changePassword(Request $request){
        $params = $request->only(['user_id','old_password','new_password']);
        $res = $this->rep->changePasswordRep($params);
        return re($res['code'],$res['data'],$res['msg']);
    }


}
